﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using caedisi.Web.DataModel;
using caedisi.Controller;

namespace caedisi.Views
{
    public partial class ShowRecordedStates : ChildWindow
    {
        public ShowRecordedStates()
        {
            InitializeComponent();

            //CellularAutomataController.Instance.LazyLoadAllCellularAutomata(cas =>
            //    {
            //        if (cas != null)
            //        {
            //            this.CellularAutomatas = new ObservableCollection<CellularAutomata>(cas);

            //            if (CellularAutomatas.Count>0)
            //                this.dataGridCas.SelectedItem = this.CellularAutomatas[0];
            //        }
            //    });
        
        }

        /// <summary>
        /// Handles the Click event of the OKButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (SelectedRecordId != 0)
                this.DialogResult = true;
            else
            {
                MessageBox.Show("You should select an item or press cancel");
            }
        }

        /// <summary>
        /// Handles the Click event of the CancelButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// Gets or sets the selected cellular automata id.
        /// </summary>
        /// <value>
        /// The selected cellular automata id.
        /// </value>
        public int SelectedRecordId 
        {
            get
            {
                RecordState rs = this.recordStateDataGrid.SelectedItem as RecordState;
                
                if(rs!=null)
                    return rs.RecordStateId;
                return 0;
            }
        
        }

        private ObservableCollection<RecordState> _RecordStates;
        /// <summary>
        /// Gets or sets the current cellular automata.
        /// </summary>
        /// <value>
        /// The current cellular automata.
        /// </value>
        public ObservableCollection<RecordState> RecordStates
        {
            get
            {
                return _RecordStates;
            }
            set
            {
                if (_RecordStates != value)
                {
                    _RecordStates = value;
                    this.RaisePropertyChanged("RecordStates");

                }
            }
        }

        #region INotifiedPropertyChange
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Raises the property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        //}

        //private void OKButton_Click(object sender, RoutedEventArgs e)
        //{
        //    this.DialogResult = true;
        //}

        //private void CancelButton_Click(object sender, RoutedEventArgs e)
        //{
        //    this.DialogResult = false;
        //}

        private void recordStateDomainDataSource_LoadedData(object sender, System.Windows.Controls.LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void recordStateDomainDataSource_LoadedData_1(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

    }
}

